package service

import (
	"go-admin/internal/model"
	mr "go-admin/internal/model/api/model_request"
	"go-admin/pkg/app"
)

func (s *Service) CreateDict(param *mr.CreateDictRequest) (string, error) {
	// 根据字典名称和字典type是否重复
	dict, err := s.dao.GetDictByDictNameAndType(param.DictName, param.Type)
	if err != nil {
		return dict.Id, err
	}
	// 判断字典是否存在
	if dict.Id != "" {
		return dict.Id, err
	}
	insertId, err := s.dao.CreateDict(param.IsSystem, param.Type, param.DictName, param.Remark)
	if err != nil {
		return "", err
	}
	return insertId, nil
}

func (s *Service) CountDict(param *mr.ListDictRequest) (int, error) {
	return s.dao.CountDict(param)
}

func (s *Service) GetDictList(param *mr.ListDictRequest, pager *app.Pager) ([]*model.Dict, error) {
	return s.dao.GetDictList(param, pager.PageIndex, pager.PageSize)
}

func (s *Service) UpdateDict(param *mr.UpdateDictRequest) error {
	return s.dao.UpdateDict(param)
}

func (s *Service) DeleteDictS(ids []string) error {
	return s.dao.DeleteDictS(ids)
}

func (s *Service) GetDictById(id string) (*model.Dict, error) {
	dict, err := s.dao.GetDictById(id)
	if err != nil {
		return nil, err
	}
	// 判断是否存在
	if dict.Id == "" {
		return nil, err
	}
	return dict, nil
}
